package com.wuhao.code.check.completion

import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiPolyVariantReference
import com.intellij.psi.PsiReferenceBase
import com.intellij.psi.ResolveResult
import com.intellij.psi.xml.XmlToken
import com.wuhao.code.check.gotohandler.resolveProperty
import org.jetbrains.kotlin.asJava.elements.KtLightField
import org.jetbrains.kotlin.idea.editor.fixers.start
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.psiUtil.startOffset

/**
 * @author 吴昊
 * @date 2024/02/01
 * @version 1.0
 * @since v1.0.0
 */
class ClassFieldReference(
    private val element: XmlToken,
    private val textRange: TextRange
) : PsiReferenceBase<PsiElement>(
    element, textRange
), PsiPolyVariantReference {

  override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> {
    return arrayOf()
  }

  override fun resolve(): PsiElement? {
    val property = resolveProperty(element, textRange.start + element.startOffset)
    if (property?.field != null) {
      if (property.field is KtLightField) {
        return property.field.kotlinOrigin
      }
      return property.field
    }
    return null
  }

}
